package com.xyc.wemedia.order.controller;

import com.github.wxpay.sdk.MyWXConfig;
import com.github.wxpay.sdk.WXPay;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.xyc.wemedia.order.service.OrderService;
import com.xyc.wemedia.order.bean.WxbOrder;
import com.xyc.wemedia.order.service.LocationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("order")
public class OrderController {
    @Resource
    private LocationService locationService;

    @Resource
    private OrderService orderService;
    @RequestMapping("save")
    public String addOrder(WxbOrder wxbOrder,HttpSession session){
        Random random = new Random();
        int nextInt = random.nextInt(100000);
        String orderId = "xy" + nextInt;
        wxbOrder.setOrderId(orderId);
        wxbOrder.setState(0);//下订单却未付款

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        wxbOrder.setOrderTime(timestamp);
        try {
            orderService.addOrder(wxbOrder);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            WXPay wxPay = new WXPay(new MyWXConfig());
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", "游戏充值");
            data.put("out_trade_no", orderId);
            data.put("device_info", "PC");
            data.put("fee_type", "CNY");
            data.put("total_fee", "1");
            data.put("spbill_create_ip", "123.12.12.123");
            //支付成功之后的回调接口
            //微信告诉微分销商户订单已付款
            data.put("notify_url", "http://nsen8z.natappfree.cc/merchant1/notify_url");
            data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
            data.put("product_id", "12");

            try {
                //下单
                Map<String, String> resp = wxPay.unifiedOrder(data);
                //System.out.println(resp);
                String payUrl = resp.get("code_url");
                session.setAttribute("payUrl",payUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

       // Message message = new Message();
        return "wemedia/pay.html";
    }
    @RequestMapping("qrcode")
    public void createQrCode(HttpSession session, HttpServletResponse response){
        String payUrl = session.getAttribute("payUrl").toString();
        //通过支付链接生成二维码
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(payUrl, BarcodeFormat.QR_CODE, 200, 200, hints);
            MatrixToImageWriter.writeToStream(bitMatrix, "PNG", response.getOutputStream());
            System.out.println("创建二维码完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
